package com.module.recall.manager;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.recall.dao.AntiFakeProductRecallDao;
import com.module.recall.vo.AntiFakeProductRecall;

/**
 * 商品召回manager类
 * 
 * @author wult
 * @Date Mar 2, 2012
 */
public class AntiFakeProductRecallManager extends
		GenericHibernateManager<AntiFakeProductRecall, Long> {
	private AntiFakeProductRecallDao dao = AntiFakeProductRecallDao
			.getInstance();
	private static AntiFakeProductRecallManager service;

	@Override
	public GenericHibernateDao<AntiFakeProductRecall, Long> getDao() {
		return dao;
	}

	public synchronized static AntiFakeProductRecallManager getInstance() {
		if (service == null) {
			service = new AntiFakeProductRecallManager();
		}
		return service;
	}

	public Page listProductRecallPage(AntiFakeProductRecall bean, Page pageObj)
			throws Exception {
		Session session = this.getSession();
		Page page = null;
		try {
			page = dao.getList(session, bean, pageObj.getStart(),
					pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}

	public boolean saveRecall(AntiFakeProductRecall bean) throws Exception {
		bean.setCreatetime(new Date());
		bean.setRegState(AntiFakeProductRecall.UNREG_STATE);
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		boolean flag = false;
		try {
			List list = dao.listByProperty(session, "codeInfo",
					bean.getCodeInfo());
			if (CollectionUtils.isEmpty(list)) {
				flag = dao.save(session, bean);
				tx.commit();
			}
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return flag;
	}
}
